package edu.fudan.service.impl;

import edu.fudan.dao.BookMeetingRoomDao;
import edu.fudan.model.BookMeetingRoom;
import edu.fudan.service.BookMeetingRoomManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;


/**
 * Implementation of UserManager interface.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Service("bookMeetingRoomManager")
@WebService(serviceName = "bookMeetingRoom", endpointInterface = "edu.fudan.service.BookMeetingRoomService")
public class BookMeetingRoomManagerImpl extends GenericManagerImpl<BookMeetingRoom, Long> implements BookMeetingRoomManager {
    private BookMeetingRoomDao bookMeetingRoomDao;

    @Autowired
    public void setBookMeetingRoomDao(BookMeetingRoomDao bookMeetingRoomDao) {
        this.dao = bookMeetingRoomDao;
        this.bookMeetingRoomDao = bookMeetingRoomDao;
    }

    /**
     * {@inheritDoc}
     */
    public BookMeetingRoom getBookMeetingRoom(String id) {
        return bookMeetingRoomDao.get(new Long(id));
    }

    /**
     * {@inheritDoc}
     */
    public List<BookMeetingRoom> getBookMeetingRooms(Long meetingRoomId) {
        return bookMeetingRoomDao.getBookMeetingRooms(meetingRoomId);
    }

    /**
     * {@inheritDoc}
     */
    public BookMeetingRoom saveBookMeetingRoom(BookMeetingRoom bookMeetingRoom) throws Exception {
        try {
            return bookMeetingRoomDao.saveBookMeetingRoom(bookMeetingRoom);
        } catch (Exception e) {
            //e.printStackTrace();
            log.warn(e.getMessage());
            throw new Exception("Book Meeting Room already exists!");
        }
    }

    /**
     * {@inheritDoc}
     */
    public void removeBookMeetingRoom(String id) {
        log.debug("removing book meeting room: " + id);
        bookMeetingRoomDao.remove(new Long(id));
    }

    /**
     * {@inheritDoc}
     *
     * @param username the login name of the human
     * @return User the populated user object
     * @throws UsernameNotFoundException thrown when username not found
     */
    public BookMeetingRoom getBookMeetingRoomById(Long id) throws Exception {
        return (BookMeetingRoom) bookMeetingRoomDao.loadBookMeetingRoomById(id);
    }
    
    /**
     * {@inheritDoc}
     */
    public List<BookMeetingRoom> search(String searchTerm) {
        return super.search(searchTerm, BookMeetingRoom.class);
    }
}
